package com.mt.api.modules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.api.constants.enums.CustomerBillTypeEnum;
import com.mt.api.modules.domain.AppCustomer;
import com.mt.api.modules.domain.AppCustomerBill;
import com.mt.api.modules.domain.bo.CustomerBillAddBo;
import com.mt.api.modules.domain.bo.CustomerBillPageBo;
import com.mt.api.modules.domain.bo.CustomerBillStatBo;
import com.mt.api.modules.domain.dto.CustomerBillStatDto;
import com.mt.api.modules.domain.vo.MerchantBillStatVo;
import com.mt.api.modules.domain.vo.CustomerBillVo;
import com.mt.api.modules.manager.ICustomerBillManager;
import com.mt.api.modules.manager.ICustomerManager;
import com.mt.api.modules.mapstruct.CustomerBillMapStruct;
import com.mt.api.modules.service.ICustomerBillService;
import com.mt.api.util.AmountUtil;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: plover
 * @Date: 2022/04/28 15:47
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class CustomerBillImpl implements ICustomerBillService {

    private final ICustomerBillManager customerBillManager;
    private final CustomerBillMapStruct customerBillMapStruct;
    private final ICustomerManager customerManager;

    @Override
    public WebResponse customerBillAdd(CustomerBillAddBo bo) {
        AppCustomerBill customerBill = new AppCustomerBill();
        customerBill.setId(bo.getId());
        customerBill.setCustomerId(bo.getCustomerId());
        customerBill.setType(CustomerBillTypeEnum.SETTLE.getCode());
        customerBill.setSettleAmount(AmountUtil.multiplyHundred(bo.getSettleAmount()));
        customerBillManager.saveOrUpdate(customerBill);
        return WebResponseUtils.success();
    }

    @Override
    public WebResponse getCustomerBillPage(CustomerBillPageBo bo) {
        Page page = customerBillManager.getCustomerBillPage(bo);
        List<AppCustomerBill> records = page.getRecords();
        List<CustomerBillVo> customerBillVoList = records.stream().map(appCustomerBill -> {
            CustomerBillVo customerBillVo = customerBillMapStruct.toVo(appCustomerBill);
            customerBillVo.setTypeText(CustomerBillTypeEnum.ofCode(customerBillVo.getType()).getName());
            return customerBillVo;
        }).collect(Collectors.toList());
        page.setRecords(customerBillVoList);
        return WebResponseUtils.successPage(page);
    }

    @Override
    public WebResponse<CustomerBillVo> getCustomerBillDetail(Integer id) {
        AppCustomerBill customerBill = customerBillManager.getById(id);
        CustomerBillVo customerBillVo = customerBillMapStruct.toVo(customerBill);
        AppCustomer customer = customerManager.getById(customerBill.getCustomerId());
        if (Objects.nonNull(customer)) {
            customerBillVo.setReceiver(customer.getReceiver());
            customerBillVo.setRphone(customer.getRphone());
            customerBillVo.setRarea(customer.getRarea());
            customerBillVo.setRaddress(customer.getRaddress());
        }
        return WebResponseUtils.success(customerBillVo);
    }


    @Override
    public WebResponse<MerchantBillStatVo> getCustomerBillStat(CustomerBillStatBo bo) {
        List<CustomerBillStatDto> billStatList = customerBillManager.getCustomerBillStat(bo.getCustomerId());
        billStatList = billStatList.stream().filter(customerBillStatDto -> customerBillStatDto.getUnSettleAmount() != 0).collect(Collectors.toList());
        Integer sum = billStatList.stream().mapToInt(value -> value.getUnSettleAmount()).sum();
        MerchantBillStatVo vo = new MerchantBillStatVo();
        vo.setUnSettleNum(billStatList.size());
        vo.setUnSettleAmount(AmountUtil.divideHundred(sum));
        return WebResponseUtils.success(vo);
    }
}
